// @file      Archive.groovy
// @author    Mac Radigan
// @purpose   archival utilities

package ycoa.archive

import java.text.*
import java.util.TimeZone

public class Archive {

 def ant = new AntBuilder()
 private String archiveRoot
 private String archiveName

 public Archive(archiveRoot, archiveName) {
   this.archiveRoot = archiveRoot
   this.archiveName = archiveName
 }

 public String getLogFilename(String dirPrefix, String dirID, String extension) {
   def destination = "${archiveRoot}/${dirPrefix}_${dirID}"
   def logFile = "${destination}/${archiveName}_${dirPrefix}_${dirID}.${extension}"
   return logFile
 }

 public File getLogFile(String dirPrefix, String dirID, String extension) {
   return new File(getLogFilename(dirPrefix, dirID, extension))
 }

 public void move(sourceFile, dirPrefix, dirID, fileID, extension) {
   def destination = "${archiveRoot}/${dirPrefix}_${dirID}"
   ant.mkdir( dir:destination )
   def stamp = getUniqueTimeStamp()
   def destinationFile = "${destination}/${archiveName}_${dirPrefix}_${dirID}_${fileID}-${stamp}.${extension}"
   ant.move( file:sourceFile, tofile:destinationFile )
 }

 public static String getUniqueTimeStamp() {
   def dateFormatter = new SimpleDateFormat( "yyyy-MM-dd" )
   dateFormatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) )
   def date = dateFormatter.format( new Date() )
   long now = System.currentTimeMillis()
   return "${date}-${now}"
 }

}

// *EOF*

